package com.yunze.system.service.impl.yunze.fhgl;

import com.yunze.common.mapper.mysql.fhgl.Ship.*;
import com.yunze.common.utils.yunze.ID_Generate;
import com.yunze.common.utils.yunze.PageUtil;
import com.yunze.system.service.yunze.fhgl.IFhglShippingApplicationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class FhglShippingApplicationServiceImpl implements IFhglShippingApplicationService {
    @Resource
    private FhglShippingApplicationMapper fhglShippingApplicationMapper;
    @Resource
    private FhglShippingApplicationFormalMapper fhglShippingApplicationFormalMapper;
    @Resource
    private FhglShippingApplicationFormalDetailsMapper fhglShippingApplicationFormalDetailsMapper;
    @Resource
    private FhglShippingApplicationInvoiceMapper fhglShippingApplicationInvoiceMapper;
    @Resource
    private FhglShippingApplicationOrdinaryMapper fhglShippingApplicationOrdinaryMapper;
    @Resource
    private FhglOrderDeliveryMapper fhglOrderDeliveryMapper;
    @Resource
    private FhglShippingApplicationOtherMapper fhglShippingApplicationOtherMapper;

    @Override
    @Transactional
    public boolean add_SAN(Map<String, Object> map){
        boolean bool = false;
        Map<String, Object> SAN_Map=(Map<String, Object>) map.get("SAN_Map");
        Integer SAN_ID_CT = fhglShippingApplicationMapper.sel_data_count();
        SAN_ID_CT=SAN_ID_CT!=null?SAN_ID_CT:0;//如果今日添加为0会反回null,给个默认值0 //FSAN 标识
        String SAN_ID= ID_Generate.G_id("FSAN", SAN_ID_CT, 4);//生成ID
        SAN_Map.put("SAN_ID", SAN_ID);//赋值id
        //手动填写时 赋值 数据
        String IsManual = SAN_Map.get("IsManual").toString();
        if(IsManual.equals("1")){
            SAN_Map.put("SAN_Recipient_Address",SAN_Map.get("Manual_SAN_Recipient_Address"));
            SAN_Map.put("SAN_Recipient_Tel",SAN_Map.get("Manual_SAN_Recipient_Tel"));
            SAN_Map.put("SAN_Recipient_Name",SAN_Map.get("Manual_SAN_Recipient_Name"));
        }
        SAN_Map.put("create_UserId",map.get("create_UserId"));//创建人
        bool = fhglShippingApplicationMapper.add_SAN(SAN_Map)>0;//添加
        map.put("SAN_ID", SAN_ID);//赋值id
        //普通发货判断
        List<Map<String, Object>> ASOY_arr= (List<Map<String, Object>>) map.get("ASOY_arr");
        if(ASOY_arr.size()>0) {
            fhglShippingApplicationOrdinaryMapper.add_ASOY(map);
        }
        //发票发货信息判断
        List<Map<String, Object>> SAIE_arr= (List<Map<String, Object>>) map.get("SAIE_arr");
        if(SAIE_arr.size()>0) {
            fhglShippingApplicationInvoiceMapper.add_SAIE(map);
        }
        //正式合同发货信息判断
        List<Map<String, Object>> SAFDS_arr= (List<Map<String, Object>>) map.get("SAFDS_arr");
        if(SAFDS_arr.size()>0) {
            Map<String, Object> SAFL_Map=(Map<String, Object>) map.get("SAFL_Map");
            SAFL_Map.put("SAN_ID", SAN_ID);//赋值id
            fhglShippingApplicationFormalMapper.add_SAFL(SAFL_Map);
            fhglShippingApplicationFormalDetailsMapper.add_SAFDS(map);
        }

        //其他发货信息判断
        List<Map<String, Object>> SAOR_arr= (List<Map<String, Object>>) map.get("SAOR_arr");
        if(SAOR_arr.size()>0) {
            fhglShippingApplicationOtherMapper.add_SAOR(map);
        }

        //合同发货 状态 修改
        List<Map<String, Object>> ODY_arr= (List<Map<String, Object>>) map.get("ODY_arr");
        if(ODY_arr.size()>0) {
            for (int i = 0; i < ODY_arr.size(); i++) {
                Map<String, Object> ODY_Map=ODY_arr.get(i);
                fhglOrderDeliveryMapper.upd_Order_Delivery(ODY_Map);
            }
        }
        return bool;
    }

    @Override
    public Map<String, Object> selMap(Map<String, Object> map)  {
        Map<String, Object> Rmp=new HashMap<String, Object>();
        Integer currenPage=map.get("pageNum")!=null?Integer.parseInt(map.get("pageNum").toString()):0;
        Integer pageSize=map.get("pageSize")!=null?Integer.parseInt(map.get("pageSize").toString()):10;
        Integer rowCount = fhglShippingApplicationMapper.selMapCount(map);
        rowCount=rowCount!=null?rowCount:0;
        PageUtil pu=new PageUtil(rowCount,currenPage , pageSize);//初始化分页工具类
        //数据打包
        map.put("StarRow", pu.getStarRow());
        map.put("PageSize", pu.getPageSize());
        Rmp.put("Pu", pu);
        Rmp.put("Data", fhglShippingApplicationMapper.selMap(map));
        return  Rmp;
    }

    @Override
    public Map<String, Object> find_data(Map<String, Object> map)  {
        Map<String, Object> R_Map = new  HashMap<String, Object>();
        //发货申请 主信息
        Map<String, Object> SAN_Map = fhglShippingApplicationMapper.find_data(map);
        R_Map.put("SAN_Map", SAN_Map);
        String SAN_Shipping_Method_arr[] =SAN_Map.get("SAN_Shipping_Method").toString().split(",");//获取发货方式数组
        //根据发货方式取出数据
        for (int i = 0; i < SAN_Shipping_Method_arr.length; i++) {
            switch (Integer.parseInt(SAN_Shipping_Method_arr[i])) {
                case 2:
                    //合同发货
                    R_Map.put("SAFL_Map",  fhglShippingApplicationFormalMapper.find_data(map));
                    R_Map.put("SAFDS_arr",fhglShippingApplicationFormalDetailsMapper.find_data(map));
                    break;
                case 3:
                    //普通发货
                    R_Map.put("ASOY_arr", fhglShippingApplicationOrdinaryMapper.find_data(map));
                    break;
                case 4:
                    //发票发货
                    R_Map.put("SAIE_arr", fhglShippingApplicationInvoiceMapper.find_data(map));
                    break;
                case 5:
                    //其他发货
                    R_Map.put("SAOR_arr", fhglShippingApplicationOtherMapper.find_data(map));
                    break;
                default:
                    break;
            }
        }
        return R_Map;
    }

    @Override
    @Transactional
    public boolean upd_SAN(Map<String, Object> map)  {
        boolean bool = false;
        Map<String, Object> SAN_Map=(Map<String, Object>) map.get("SAN_Map");

        //手动填写时 赋值 数据
        String IsManual = SAN_Map.get("IsManual").toString();
        if(IsManual.equals("1")){
            SAN_Map.put("SAN_Recipient_Address",SAN_Map.get("Manual_SAN_Recipient_Address"));
            SAN_Map.put("SAN_Recipient_Tel",SAN_Map.get("Manual_SAN_Recipient_Tel"));
            SAN_Map.put("SAN_Recipient_Name",SAN_Map.get("Manual_SAN_Recipient_Name"));
        }
        bool = fhglShippingApplicationMapper.upd_SAN(SAN_Map)>0;//修改发货申请
        map.put("SAN_ID", SAN_Map.get("SAN_ID"));//赋值id
        //新增前先删除
        fhglShippingApplicationOrdinaryMapper.del_date(map);
        fhglShippingApplicationInvoiceMapper.del_date(map);
        fhglShippingApplicationOtherMapper.del_date(map);
        fhglShippingApplicationFormalMapper.del_date(map);
        fhglShippingApplicationFormalDetailsMapper.del_date(map);

        //普通发货判断
        List<Map<String, Object>> ASOY_arr= (List<Map<String, Object>>) map.get("ASOY_arr");
        if(ASOY_arr.size()>0) {
            fhglShippingApplicationOrdinaryMapper.add_ASOY(map);
        }
        //发票发货信息判断
        List<Map<String, Object>> SAIE_arr= (List<Map<String, Object>>) map.get("SAIE_arr");
        if(SAIE_arr.size()>0) {
            fhglShippingApplicationInvoiceMapper.add_SAIE(map);
        }
        //其他发货信息判断
        List<Map<String, Object>> SAOR_arr= (List<Map<String, Object>>) map.get("SAOR_arr");
        if(SAOR_arr.size()>0) {
            fhglShippingApplicationOtherMapper.add_SAOR(map);
        }
        //正式合同发货信息判断
        List<Map<String, Object>> SAFDS_arr= (List<Map<String, Object>>) map.get("SAFDS_arr");
        if(SAFDS_arr.size()>0) {
            Map<String, Object> SAFL_Map=(Map<String, Object>) map.get("SAFL_Map");
            SAFL_Map.put("SAN_ID", map.get("SAN_ID"));//赋值id
            fhglShippingApplicationFormalMapper.add_SAFL(SAFL_Map);
            fhglShippingApplicationFormalDetailsMapper.add_SAFDS(map);
        }
        return bool;
    }

    @Override
    @Transactional
    public boolean del_SAN(Map<String, Object> map)  {
        //删除
        fhglShippingApplicationOrdinaryMapper.del_date(map);
        fhglShippingApplicationInvoiceMapper.del_date(map);
        fhglShippingApplicationOtherMapper.del_date(map);
        fhglShippingApplicationFormalMapper.del_date(map);
        fhglShippingApplicationFormalDetailsMapper.del_date(map);
        return fhglShippingApplicationMapper.del_SAN(map)>0;
    }

    @Override
    public List<Map<String, Object>> sel_SUM_Sent_Quantity(Map<String, Object> map)  {
        List<Map<String, Object>> R_Map= null;
        if(map.get("type").equals("CT")) {
            R_Map = fhglShippingApplicationFormalMapper.sel_SUM_Sent_Quantity(map);
        }else {
           // R_Map = FHGL_Shipping_Application_Temporary_Mapper.sel_SUM_Sent_Quantity( map);
        }
        return R_Map;
    }

    @Override
    public List<String> sel_SAN_ID(Map<String, Object> map)  {
        List<String> R_Map= null;
        if(map.get("ODY_nocID").toString().equals("1")) {
            R_Map = fhglShippingApplicationFormalMapper.sel_SAN_ID( map);
        }else {
            //R_Map = (List<String>) dao.findForList("FHGL_Shipping_Application_Temporary_Mapper.sel_SAN_ID", map);
        }
        return R_Map;
    }

    @Override
    public List<Map<String,Object>> sleNumber(Map map) {
        return fhglShippingApplicationMapper.sleNumber(map);
    }

    @Override
    public Map<String, Object> find_OutData(Map<String, Object> map) {
        Map<String, Object> R_Map = new  HashMap<String, Object>();
        //发货申请 主信息
        Map<String, Object> SAN_Map = fhglShippingApplicationMapper.find_data(map);
        R_Map.put("SAN_Map", SAN_Map);
        String SAN_Shipping_Method_arr[] =SAN_Map.get("SAN_Shipping_Method").toString().split(",");//获取发货方式数组
        //根据发货方式取出数据
        for (int i = 0; i < SAN_Shipping_Method_arr.length; i++) {
            switch (Integer.parseInt(SAN_Shipping_Method_arr[i])) {
                case 2:
                    //合同发货
                    R_Map.put("SAFDS_arr",fhglShippingApplicationFormalDetailsMapper.find_out(map));
                    break;
                case 3:
                    //普通发货
                    R_Map.put("ASOY_arr", fhglShippingApplicationFormalDetailsMapper.find_arr(map));
                    break;
                default:
                    break;
            }
        }
        return R_Map;
    }




}
















